package com.shanghaiyimu.yimiyangche.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.fragment.DianpuFragment;
import com.shanghaiyimu.yimiyangche.fragment.WenDaFragment;
import com.shanghaiyimu.yimiyangche.fragment.KehuFragment;
import com.shanghaiyimu.yimiyangche.fragment.ShouyeFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout shouye;
    private RelativeLayout mendian;
    private RelativeLayout faxian;
    private RelativeLayout wode;

    Fragment f1,f2,f3,f4;

    //定义要用的颜色值
    private int whirt = 0xFFFFFFFF;
    private int gray = 0xFF737373;
    private int blue =0xFF0AB2FB;

    FragmentManager manager;
    FragmentTransaction transaction;
    private ImageView shouyeimg,mendianimg,faxianimg,wodeimg;
    private TextView shouyetxt,mendiantxt,faxiantxt,wodetxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setOnClickListener();
    }

    private void setOnClickListener() {


        transaction.add(R.id.frame_layout_main, f1);
        transaction.add(R.id.frame_layout_main, f2);
        transaction.add(R.id.frame_layout_main, f3);
        transaction.add(R.id.frame_layout_main, f4);




        transaction.show(f1);
        transaction.hide(f2);
        transaction.hide(f3);
        transaction.hide(f4);

        transaction.commit();

    }

    private void initView() {

        f1 = new ShouyeFragment();
        f2 = new DianpuFragment();
        f3 = new WenDaFragment();
        f4 = new KehuFragment();

        shouyeimg = (ImageView) findViewById(R.id.radio_button_shouye);
        shouyetxt = (TextView) findViewById(R.id.radio_button_shouye_text);
        mendianimg = (ImageView) findViewById(R.id.radio_button_mendian);
        mendiantxt = (TextView) findViewById(R.id.radio_button_mendian_text);
        faxianimg = (ImageView) findViewById(R.id.radio_button_faxian);
        faxiantxt = (TextView) findViewById(R.id.radio_button_faxian_text);
        wodeimg = (ImageView) findViewById(R.id.radio_button_kehu);
        wodetxt = (TextView) findViewById(R.id.radio_button_kehu_text);

        shouye = (RelativeLayout) findViewById(R.id.shouye_layout);
        mendian = (RelativeLayout) findViewById(R.id.mendian_layout);
        faxian = (RelativeLayout) findViewById(R.id.faxian_layout);
        wode = (RelativeLayout) findViewById(R.id.wode_layout);

        shouye.setOnClickListener(this);
        mendian.setOnClickListener(this);
        faxian.setOnClickListener(this);
        wode.setOnClickListener(this);

        shouyetxt.setTextColor(blue);
        shouyeimg.setEnabled(false);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

    }

    @Override
    public void onClick(View v) {

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        switch (v.getId()) {
            case R.id.shouye_layout:

                setChioceItem(0);
                break;
            case R.id.mendian_layout:

                setChioceItem(1);
                break;
            case R.id.faxian_layout:


                setChioceItem(2);
                break;
            case R.id.wode_layout:
                setChioceItem(3);


                break;
        }
        transaction.commit();

    }



    //定义一个选中一个item后的处理
    public void setChioceItem(int index)
    {
        //重置选项+隐藏所有Fragment
        FragmentTransaction transaction = manager.beginTransaction();
        clearChioce();
        hideFragments(transaction);
        switch (index) {
            case 0:
                shouyeimg.setEnabled(false);
                shouyetxt.setTextColor(blue);
                if (f1 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    f1 = new ShouyeFragment();
                    transaction.add(R.id.frame_layout_main, f1);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(f1);
                }
                break;

            case 1:
                mendianimg.setEnabled(false);
                mendiantxt.setTextColor(blue);
                if (f2 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    f2 = new DianpuFragment();
                    transaction.add(R.id.frame_layout_main, f2);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(f2);
                }
                break;

            case 2:
                faxianimg.setEnabled(false);
                faxiantxt.setTextColor(blue);
                if (f3 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    f3 = new DianpuFragment();
                    transaction.add(R.id.frame_layout_main, f3);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(f3);
                }
                break;
            case 3:
                wodeimg.setEnabled(false);
                wodetxt.setTextColor(blue);
                if (f4 == null) {
                    // 如果fg1为空，则创建一个并添加到界面上
                    f4 = new DianpuFragment();
                    transaction.add(R.id.frame_layout_main, f4);
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(f4);
                }
                break;
        }
        transaction.commit();
    }

    //隐藏所有的Fragment,避免fragment混乱
    private void hideFragments(FragmentTransaction transaction) {
        if (f1 != null) {
            transaction.hide(f1);
        }
        if (f2 != null) {
            transaction.hide(f2);
        }
        if (f3 != null) {
            transaction.hide(f3);
        }
        if (f4 != null) {
            transaction.hide(f4);
        }
    }


    //定义一个重置所有选项的方法
    public void clearChioce()
    {
        shouyeimg.setEnabled(true);
        shouyetxt.setTextColor(gray);
        mendianimg.setEnabled(true);
        mendiantxt.setTextColor(gray);
        faxianimg.setEnabled(true);
        faxiantxt.setTextColor(gray);
        wodeimg.setEnabled(true);
        wodetxt.setTextColor(gray);
    }
}